<?php

class PunchmachineController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;


	public function init() {

		$this->commonAction();	

	} 
	//查询机构考勤卡情况列表	
	 public function indexAction() {
		try {  	
				  
		     $where='where 1';	
		     $groupid = ( int ) $this->_request->getParam ( 'groups' );
		     $school_id = $this->_request->getParam ( 'school' ); 		
		     $this->view->groups=$groupid; 
		     $this->view->schoolid=$schoolid;  
			 $outdatetime=time()-900;
			 $this->view->outdatetime= $outdatetime;
			 if($school_id){ 
				 $schoolarray=explode('_',$school_id);  
				 $schoolid=$schoolarray['1'];
			 }
			   
			 if($groupid){
				 $where .=' and  a.groupid="'.$groupid.'" ';
				 $this->view->groupid= $groupid; 
			 }
				 
			 if($schoolid){
				 $where .=' and  a.schoolid="'.$schoolid.'" '; 
				 $this->view->schoolid= $school_id; 
			 } 
			 $sql='select  a.schoolid,a.schoolname,b.groupid,b.groupname from school a left join groups b on a.groupid=b.groupid '.$where;	
			 $school=$this->_dbAdapter->fetchAssoc ($sql); 
			 $this->view->school= $school;
			 if($schoolid || $groupid){
			 	$where1=' where schoolid in ('.implode(',',array_keys($school)).')';
			 }
			
			$count = 10;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			$this->view->pageNum= $curpage;			
    		$offset = ($curpage - 1) * $count; 

			 $sql="SELECT SQL_CALC_FOUND_ROWS id,mid,schoolid,updatetime,version from punch_machine ".$where1." order BY schoolid asc limit $offset,$count";
			 //echo $sql;
			 $result = $this->_dbAdapter->fetchAssoc ($sql);  
			 $this->view->result= $result;
			 //print_r($result);print_r($outdatetime);exit;
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    			$page->setvar ( array ('groups' => $groupid,'school'=>$schoolid,'class'=>$classid) );
				$this->view->page= $page->getNumPage ();
		} catch ( Exception $e ) {
			 $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	 public function addAction() {
		try { 
		    $sid = $this->_request->getParam ( 'sid' );  
		    $gid = $this->_request->getParam ( 'gid' ); 
	    	
			if($sid){
				$this->view->school=$gid.'_'.$sid;
			}
			if($gid){
		     $this->view->groups=$gid; 
			}
			
		
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	 public function opaddAction() {
		try { 
			if(! $this->isPost ()){
				throw new Exception ( '参数错误！' );
			}
			$p=$this->_request->getPost();
			
			//print_r($p);exit;
			if(empty($p['groups'])){
				throw new Exception ( '请选择机构！' );
			}
			if($p['school']){ 
				 $schoolarray=explode('_',$p['school']);  
				 $schoolid=$schoolarray['1'];
				 $groupid=$schoolarray['0'];
			}else{
				 throw new Exception ( '请选择学校！' );
			}
			if(empty($p['mid'])){
				throw new Exception ( '请填考勤机ID！' );
			}

			$sql="select * from punch_machine where mid='".trim($p['mid'])."' ";
			$mid=$this->_dbAdapter->fetchAll($sql);

			if($mid){
				throw new Exception ( '此卡号已存在！' );
			}
			$data=array(
				'mid'=>trim($p['mid']),
				'schoolid'=>$schoolid,
				'updatetime'=>time()
			);
		    $res=$this->_dbAdapter->Insert('punch_machine',$data);
		    if($res){
			  $this->feedback ( '成功', '注意', '/'.$this->controller.'/index', 'tip',0);		 
			}else{
			   $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
			}  
		
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	
	//查询已发考勤卡列表	
	 public function stateAction() {
		try {  	

			 $sql='select  * from healthstate where 1';	
			 $state=$this->_dbAdapter->fetchAll ($sql); 
			 $this->view->state= $state;
			 
		} catch ( Exception $e ) {
			 $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	 public function deleteAction() {
		try { 
				$id =  $this->_request->getParam ( 'id' );  
				
				if(empty($id)){
				   throw new Exception ( '参数错误！' ); 
				} 
				
			 	$res=$this->_dbAdapter->update ('healthstate',array('state'=>''),'id="'.$id.'"'); 
			    if($res){
				   $this->feedback ( '已删除', '注意', '/'.$this->controller.'/state', 'tip',0); 		 
				}else{
				   $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
				}  
	 } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	 }

	 public function editAction() {
		try { 
				$id =  $this->_request->getParam ( 'id' );  
				if(empty($id)){
				   throw new Exception ( '参数错误！' ); 
				} 
			 	$res=$this->_dbAdapter->fetchRow ('select * from healthstate where id='.$id); 
			 	$this->view->res= $res;

	 } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	 }
	 public function opeditAction() {
		try { 
			if(! $this->isPost ()){
				throw new Exception ( '参数错误！' );
			}
			$p=$this->_request->getPost();
			
			 	$res=$this->_dbAdapter->update ('healthstate',array('state'=>$p['state']),'id="'.$p['id'].'"'); 
			    if($res){
				   $this->feedback ( '修改成功', '注意', '/'.$this->controller.'/state', 'tip',0); 		 
				}else{
				   $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
				}  
	 } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	 }

	 public function pictureAction() {
		try {  	
				  
			 $sql='select filepath from punch_machine_picture where id=1 ';	
			 $picture=$this->_dbAdapter->fetchOne ($sql); 
			 $this->view->picture= $picture;

		} catch ( Exception $e ) {
			 $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function opchangeAction(){
        try {
		  		//print_r($_FILES['pic']);exit;
				//http://cloud.yunxiaoche.com/article/49376_1398221954_28738.jpg
				if(!empty($_FILES['pic']['tmp_name'])){//图片
                    $ext = explode('.', $_FILES['pic']['name']);
                    $imgname = "punchmachine_".time().'_'.$_FILES['pic']['size'];
                    $tmppath = APPLICATION_PATH.'/../tmp/letter/';
                    $smallfilename = $imgname.'.jpg.small.jpg';
                    $smallfileurl = $tmppath.$smallfilename;
                    $bigfilename = $imgname.'.jpg.big.jpg';					
                    $bigfileurl = $tmppath.$bigfilename;
					
                    $this->ThumbRatio($_FILES['pic']['tmp_name'],$smallfileurl,'227','128',1);
                    $this->ThumbRatio($_FILES['pic']['tmp_name'],$bigfileurl,'1136','640',1);
                    if(file_exists($smallfileurl)&&file_exists($bigfileurl)){
					   $res1 = Azure_Storage::defaultImg('letter', $smallfileurl, $imgname.'.jpg.small.jpg');
					   $res2 = Azure_Storage::defaultImg('letter', $bigfileurl, $imgname.'.jpg');
					   $data = array(
						  'filepath'=>'http://cloud.yunxiaoche.com/letter/'.$imgname.'.jpg',
					   );
					   $res=$this->_dbAdapter->Update('punch_machine_picture',$data,'id=1');
					   if(!$res){
						  throw new Exception ('失败');
					   }
							  
					   $res =1;
					}
				}else{
					  throw new Exception ('参数错误');

				}
				
			    if($res){
				   $this->feedback ( '成功', '注意', '/'.$this->controller.'/picture', 'tip',0); 		 
				}else{
				   $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
				}  
	    } catch ( Exception $e ) {
		   $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
	    }
    }
	

   public function groupsAction() {
		try { 
		 $p =$this->_request->getPost ();
         $sql="select * from  groups ";
	     $result = $this->_dbAdapter->fetchAll ($sql);
		 
		 $str.='<option value="0" >==请选择机构==</option>';
		 for($i = 0; $i<count($result); $i++)
		{
			$str .= "<option";
			if($p['jigouid']==$result[$i]['groupid']){
				$str .=" selected=selected ";
			}
	      $str .= " value='".$result[$i]['groupid']."'>".$result[$i]['groupname']."</option>";
		}
		  
	    echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//获取所有的校园列表
	  public function schoollistAction() {
		try { 
           if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
		  $p =$this->_request->getPost (); 
		   
		  $array=explode('_',$p['sid']);
		  
		  $sql="SELECT * FROM `groups` where groupid='".$p['id']."' ";
		  $result = $this->_dbAdapter->fetchRow ($sql);
		    $sql="select * from  school where groupid='".$p['id']."' ";   
			$schoollist=$this->_dbAdapter->fetchAll ($sql);
	                 
            $str.='<option value="0" >==请选择学校==</option>';
			// echo $p['id'];   exit;
		 for($i = 0; $i<count($schoollist); $i++)
		{
		  $str .= "<option ";
		  
		  if ($array['1']==$schoollist[$i]['schoolid']){
		      $str .=" selected=selected";
		  }
		   $str .= " value='".$p['id'].'_'.$schoollist[$i]['schoolid']."'>";
		   $str .= $schoollist[$i]['schoolname']."</option>";
		   
		}
		   
	      echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	//获取所有的班级信息
	  public function classlistAction() {
		try { 
           if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
		    $p =$this->_request->getPost ();  
		    $array=explode('_',$p['id']); 
			 
		    $sql="SELECT * FROM `groups` where groupid='".$array['0']."' ";
		    $result = $this->_dbAdapter->fetchRow ($sql);
	      
		  $sql="select * from  class  where isdel=0 and schoolid='".$array[1]."' ";   
		  $classlist= $this->_dbAdapter->fetchAll ($sql);
	      $str.='<option value="0" >==请选择班级==</option>';
		  	 
		 for($i = 0; $i<count($classlist); $i++)
		{
		  $str .= "<option ";
		  if($p['cid']==$classlist[$i]['classid']){
		     $str .=" selected=selected";
		  } 
		  $str .= "  value='".$classlist[$i]['classid']."'>".$classlist[$i]['classname']."</option>";
		    
		} 
		   
	      echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
  /*
  操作处理end
  */	 
	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}}

